<template>
  <div class="pic-box">
    <div class="pic-view">
      <el-image v-if="showImg" style="width: 100%;height: 100%;" :src="imgUrl" fit="contain"></el-image>
      <video v-else :src="imgUrl" style="width: 100%;height: 100%;" controls>
        您的浏览器不支持 video 标签。
      </video>
    </div>
    <div class="pic-controls">
      <div class="pic-pre" @click="handlePrev">
        <el-icon>
          <ArrowLeftBold />
        </el-icon>
      </div>
      <div class="pic-list">
        <el-scrollbar ref="scrollbarRef" always @scroll="scroll">
          <div class="scrollbar-flex-content" ref="innerRef">
            <div class="scrollbar-item" v-for="(item, i) in picList" :class="{ active: i === current }" :key="i"
              @click="handleItemClick(i)">
              <video v-if="item.endsWith('mp4') || item.endsWith('ogg')" :src="item" style="user-select: none; width: 100%;height: 100%;">
                您的浏览器不支持 video 标签。
              </video>
              <el-image v-else style="width: 100%;height: 100%;" :src="item" fit="cover"></el-image>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <div class="pic-next" @click="handleNext">
        <el-icon>
          <ArrowRightBold />
        </el-icon>
      </div>
    </div>
  </div>
</template>

<script setup name="goodsPic">

const props = defineProps({
  picList: {
    type: Array,
    required: true,
    default: () => []
  }
})
const innerRef = ref()
const scrollbarRef = ref()
const current = ref(0)
const imgUrl = computed(() => {
  return props.picList[current.value] || ''
})
const showImg = computed(() => {
  const type = imgUrl.value.slice(-3)
  return type === 'mp4' || type === 'ogg' ? false : true
})
const handleItemClick = (index) => {
  current.value = index
  inputSlider()
}
const max = ref(0)
const value = ref(0)
onMounted(() => {
  max.value = innerRef.value?.clientWidth - 390
})
const scroll = ({ scrollLeft }) => {
  value.value = scrollLeft
}
const inputSlider = () => {
  let value = 0
  const width = 68 + 14
  const maxIndex = props.picList.length - 3
  if (current.value > 2 && current.value < maxIndex) {
    value = (current.value - 2) * width
  } else if (current.value >= maxIndex) {
    value = maxIndex * width
  }
  console.log(value)
  scrollbarRef.value?.scrollTo({
    left: value,
    behavior: 'smooth'
  })
}
const handlePrev = () => {
  if (current.value === 0) return
  current.value -= 1
  inputSlider()
}
const handleNext = () => {
  if (current.value === props.picList.length - 1) return
  current.value += 1
  inputSlider()
}
</script>

<style lang="scss" scoped>
.pic-box {
  .pic-view {
    width: 450px;
    height: 450px;
    border: 1px solid #EBEBEB;
  }

  .pic-controls {
    display: flex;
    align-items: stretch;
    margin-top: 20px;
    width: 450px;
  }

  & .pic-pre,
  & .pic-next {
    width: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 26px;
    color: var(--jbc-border-color);

    &:hover {
      color: var(--jbc-font-color);
    }
  }

  & .pic-list {
    display: flex;
    flex: 1;
    width: 1px;

    & :deep(.el-scrollbar) {

      .el-scrollbar__bar.is-horizontal,
      .el-scrollbar__bar.is-vertical {
        display: none !important;
      }
    }

    .scrollbar-flex-content {
      display: flex;
    }

    .scrollbar-item {
      flex-shrink: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 68px;
      height: 68px;
      cursor: pointer;

      &~.scrollbar-item {
        margin-left: 14px;
      }
    }
  }
}
</style>